//
//  TakeSelfieView.swift
//  GreenDeal
//
//  Created by Moennig Q on 20/2/2023.
//

import UIKit
import SnapKit
class FaceDetectSelfieView: UIView {
    var switchModeClosure: (()->Void)?
    var selfiSource: PhotoSource = .autoRecognition
    let topBackView = UIView()
    lazy var titleLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor = .white
        label.setFont(R.font.interBold(size: 16))
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    lazy var swithCamaraBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "change_camera"), for: UIControl.State.normal)
        return btn
    }()
    //切换Auto
    lazy var switchAutoBtn:  BaseButton = {
        let btn = BaseButton()
        btn.setTitle(ScanAddMethodMode.auto.rawValue, for: UIControl.State.normal)
        btn.setTitleColor(UIColor(hexStr: "#FAB815"), for: UIControl.State.normal)
        btn.setFont(R.font.interMedium(size: 14))
        btn.backgroundColor = UIColor.clear
        btn.clipsToBounds = true
        return btn
    }()
    //切换Manually
    lazy var switchManuallyBtn:  BaseButton = {
        let btn = BaseButton()
        btn.setTitle(ScanAddMethodMode.manually.rawValue, for: UIControl.State.normal)
        btn.setTitleColor(UIColor.white.withAlphaComponent(0.4), for: UIControl.State.normal)
        btn.setFont(R.font.interMedium(size: 14))
        btn.backgroundColor = UIColor.clear
        btn.clipsToBounds = true
        return btn
    }()
    //手动拍照时显示
    lazy var takePhotoBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "pvd_detail_selfie_takephoto"), for: UIControl.State.normal)
        return btn
    }()
    lazy var returnBtn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    
    let tipLabel = BaseLabel()
    lazy var noAvailableDeviceLabel: BaseLabel = {
        let label = BaseLabel()
        label.text = R.string.localizable.noCameraAvailable()
        label.textColor = .red
        label.setFont(R.font.sfProTextSemibold(size: 20))
        label.backgroundColor = .clear
        return label
    }()
    //
    //自拍用到的变量
    var recognizerViewSize: CGFloat = {
        if UIDevice.getCurrentDeviceType() == APPConfig.iPhoneType{
            return APPConfig.WidthForPortrait - 35 * 2
        }else{
            return APPConfig.WidthForPortrait / 5 * 3
        }
    }()
    let backViewTopMargin: CGFloat = 88
    let popoverBGViewTopMargin: CGFloat = 34
    let popoverBGViewHeight: CGFloat = 34
    let popoverBGViewBottomMargin: CGFloat = 34
    //
    var switchAutoBtnCenterXConstraint: Constraint?
    let switchBtnSize = CGSize(width: 80, height: 50)
    var cropRect: CGRect!
    //自拍
    lazy var backView: UIView = {
        let aView = UIView(frame: CGRect(x: 0, y: backViewTopMargin, width: APPConfig.WidthForPortrait, height: APPConfig.HeightForPortrait - backViewTopMargin))
        aView.backgroundColor = UIColor.clear
        //cropLayer
        self.cropRect = CGRect(x: (APPConfig.WidthForPortrait - recognizerViewSize) / 2, y: popoverBGViewTopMargin + popoverBGViewHeight + popoverBGViewBottomMargin, width: recognizerViewSize, height: recognizerViewSize)
        let cropLayer = CAShapeLayer()
        let path = CGMutablePath()
        path.addRect(aView.bounds)
        path.addEllipse(in: self.cropRect )
        cropLayer.path = path
        aView.layer.addSublayer(cropLayer)
        cropLayer.fillRule = CAShapeLayerFillRule.evenOdd
        cropLayer.fillColor = UIColor.black.withAlphaComponent(0.8).cgColor
        return aView
    }()
    let selfieOneLabel = ContentLabel(hexStr: "#FFFFFFCC")
    let selfieTwoLabel = ContentLabel(hexStr: "#FFFFFFCC")
    let selfieThreeLabel = ContentLabel(hexStr: "#FFFFFFCC")
    
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    private func setUpView() {
        //
        topBackView.backgroundColor = .black
        self.addSubview(topBackView)
        topBackView.snp.makeConstraints({ make in
            make.left.top.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(88)
        })
        //title
        topBackView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(48)
            make.height.greaterThanOrEqualTo(20)
            make.bottom.equalTo(-15)
        }
        //return btn
        returnBtn.setImage(R.image.navigation_bar_back_white_icon(), for: UIControl.State.normal)
        topBackView.addSubview(returnBtn)
        returnBtn.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.width.height.equalTo(40)
            make.centerY.equalTo(titleLabel)
        }
        
        //switch auto or manually
        topBackView.addSubview(swithCamaraBtn)
        swithCamaraBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalTo(-8)
            make.height.width.equalTo(40)
        }
        //
        //没有有效设备的提示信息
        self.addSubview(noAvailableDeviceLabel)
        noAvailableDeviceLabel.snp.makeConstraints { (make) in
            make.width.height.greaterThanOrEqualTo(0)
            make.center.equalToSuperview()
        }
        //总的total back view，所有的自拍UI放在backview中，居中显示
        self.addSubview(backView)
        //
        //2.中间的
        //popoverBG
        let popoverBGView = UIView()
        backView.addSubview(popoverBGView)
        popoverBGView.snp.makeConstraints { make in
            make.top.equalTo(34)
            make.centerX.equalToSuperview()
            make.left.greaterThanOrEqualTo(20)
            make.right.lessThanOrEqualTo(-20)
            make.height.greaterThanOrEqualTo(0)
        }
        //tipImageView
        let tipImageView = UIImageView(image: UIImage(named: "checklist_takephoto_tip"))
        popoverBGView.addSubview(tipImageView)
        tipImageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.width.height.equalTo(16)
            
        }
        //tipLabel
        tipLabel.textColor = UIColor(hexStr: "#FF9505")
        tipLabel.textAlignment = .left
        tipLabel.numberOfLines = 0
        tipLabel.setFont(R.font.sfProTextRegular(size: 14))
        popoverBGView.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(tipImageView.snp.right).offset(12)
            make.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(tipImageView)
            make.bottom.equalToSuperview()
        }
        
        let recognizerView = UIView()
        recognizerView.layer.cornerRadius =  recognizerViewSize / 2
        recognizerView.clipsToBounds = true
        recognizerView.backgroundColor = .clear
        backView.addSubview(recognizerView)
        recognizerView.snp.makeConstraints { make in
            make.top.equalTo(popoverBGView.snp.bottom).offset(popoverBGViewBottomMargin)
            make.width.height.equalTo(recognizerViewSize)
            make.centerX.equalToSuperview()
        }
        //switch camera
        backView.addSubview(switchAutoBtn)
        switchAutoBtn.snp.makeConstraints { (make) in
            make.top.equalTo(recognizerView.snp.bottom).offset(5)
            self.switchAutoBtnCenterXConstraint = make.centerX.equalToSuperview().constraint
            make.size.equalTo(switchBtnSize)
        }
        //switch camera
        backView.addSubview(switchManuallyBtn)
        switchManuallyBtn.snp.makeConstraints { (make) in
            make.top.equalTo(recognizerView.snp.bottom).offset(5)
            make.left.equalTo(switchAutoBtn.snp.right)
            make.size.equalTo(switchBtnSize)
        }
        //take photo : 手动拍照时显示
        backView.addSubview(takePhotoBtn)
        takePhotoBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(-(UIDevice.getBottomAreaHeight() + 20))
            make.centerX.equalToSuperview()
            make.height.width.equalTo(70)
        }
        self.takePhotoBtn.isHidden = true
        self.switchAutoBtn.addTarget(self, action: #selector(switchAutoBtnClick), for: UIControl.Event.touchUpInside)
        self.switchManuallyBtn.addTarget(self, action: #selector(switchManuallyBtnClick), for: UIControl.Event.touchUpInside)
    }
    //MARK: - 切换自动识别或手动增加
    @objc private func switchAutoBtnClick(){
        if self.selfiSource == .takePhoto {
            self.selfiSource = .autoRecognition
            self.takePhotoBtn.isHidden = true
            self.switchAutoBtn.setTitleColor(UIColor(hexStr: "#FAB815"), for: UIControl.State.normal)
            self.switchManuallyBtn.setTitleColor(UIColor.white.withAlphaComponent(0.4), for: UIControl.State.normal)
            self.switchAutoBtnCenterXConstraint?.update(offset:0)
            self.switchModeClosure?()
        }
    }
    //MARK: - 切换自动识别或手动增加
    @objc private func switchManuallyBtnClick(){
        if self.selfiSource == .autoRecognition{
            self.selfiSource = .takePhoto
            self.takePhotoBtn.isHidden = false
            self.switchManuallyBtn.setTitleColor(UIColor(hexStr: "#FAB815"), for: UIControl.State.normal)
            self.switchAutoBtn.setTitleColor(UIColor.white.withAlphaComponent(0.4), for: UIControl.State.normal)
            self.switchAutoBtnCenterXConstraint?.update(offset: (-(self.switchBtnSize.width)))
            self.switchModeClosure?()
        }
    }
    //MARK: - 设置自拍时的UI
    func setSelfieTipInfo(installerName: String,checklistTitle: String){
        self.titleLabel.text = checklistTitle
        tipLabel.text = "Installer \(installerName) must be presented in the selfie photo."
    }

}
